package model

import (
	"time"

	"gorm.io/gorm"
)

// Menu 菜单模型
type Menu struct {
	ID         uint           `gorm:"primarykey" json:"id"`
	CreatedAt  time.Time      `json:"created_at"`
	UpdatedAt  time.Time      `json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"index" json:"-"`
	ParentID   uint           `gorm:"default:0;comment:父菜单ID" json:"parent_id"`
	Path       string         `gorm:"size:128;comment:前端路由路径" json:"path"`
	Name       string         `gorm:"size:50;comment:路由名称" json:"name"`
	Component  string         `gorm:"size:128;comment:前端组件路径" json:"component"`
	Redirect   string         `gorm:"size:128;comment:重定向路径" json:"redirect"`
	Title      string         `gorm:"size:50;not null;comment:菜单标题" json:"title"`
	Icon       string         `gorm:"size:50;comment:图标" json:"icon"`
	Sort       int            `gorm:"default:0;comment:排序" json:"sort"`
	Hidden     bool           `gorm:"default:false;comment:是否隐藏" json:"hidden"`
	Type       int            `gorm:"default:0;comment:菜单类型：0目录 1菜单 2按钮" json:"type"`
	Cache      bool           `gorm:"default:false;comment:是否缓存" json:"cache"`
	Status     int            `gorm:"default:1;comment:状态(1:启用,2:禁用)" json:"status"`
	Permission string         `gorm:"size:128;comment:权限标识" json:"permission"`
	ActiveMenu string         `gorm:"size:128;comment:选中的菜单" json:"active_menu"`
	Breadcrumb bool           `gorm:"default:true;comment:是否显示面包屑" json:"breadcrumb"`
	// 关联
	Roles []Role `gorm:"many2many:go_role_menu;" json:"roles,omitempty"`
}

// TableName 设置表名
func (Menu) TableName() string {
	return "go_menu"
}

// MenuSearchParams 菜单搜索参数
type MenuSearchParams struct {
	Title  string `form:"title" json:"title"`
	Path   string `form:"path" json:"path"`
	Status int    `form:"status" json:"status"`
	Type   int    `form:"type" json:"type"`
}

// CreateMenuRequest 创建菜单请求
type CreateMenuRequest struct {
	ParentID   uint   `json:"parent_id" binding:"omitempty,gte=0"`
	Path       string `json:"path" binding:"omitempty,max=128"`
	Name       string `json:"name" binding:"omitempty,max=50"`
	Component  string `json:"component" binding:"omitempty,max=128"`
	Redirect   string `json:"redirect" binding:"omitempty,max=128"`
	Title      string `json:"title" binding:"required,max=50"`
	Icon       string `json:"icon" binding:"omitempty,max=50"`
	Sort       int    `json:"sort" binding:"omitempty,gte=0"`
	Hidden     bool   `json:"hidden"`
	Type       int    `json:"type" binding:"omitempty,oneof=0 1 2"`
	Cache      bool   `json:"cache"`
	Status     int    `json:"status" binding:"omitempty,oneof=1 2"`
	Permission string `json:"permission" binding:"omitempty,max=128"`
	ActiveMenu string `json:"active_menu" binding:"omitempty,max=128"`
	Breadcrumb bool   `json:"breadcrumb"`
}

// UpdateMenuRequest 更新菜单请求
type UpdateMenuRequest struct {
	ParentID   uint   `json:"parent_id" binding:"omitempty,gte=0"`
	Path       string `json:"path" binding:"omitempty,max=128"`
	Name       string `json:"name" binding:"omitempty,max=50"`
	Component  string `json:"component" binding:"omitempty,max=128"`
	Redirect   string `json:"redirect" binding:"omitempty,max=128"`
	Title      string `json:"title" binding:"omitempty,max=50"`
	Icon       string `json:"icon" binding:"omitempty,max=50"`
	Sort       int    `json:"sort" binding:"omitempty,gte=0"`
	Hidden     *bool  `json:"hidden" binding:"omitempty"`
	Type       int    `json:"type" binding:"omitempty,oneof=0 1 2"`
	Cache      *bool  `json:"cache" binding:"omitempty"`
	Status     int    `json:"status" binding:"omitempty,oneof=1 2"`
	Permission string `json:"permission" binding:"omitempty,max=128"`
	ActiveMenu string `json:"active_menu" binding:"omitempty,max=128"`
	Breadcrumb *bool  `json:"breadcrumb" binding:"omitempty"`
}
